package com.hmx.openplatform.gateway.listener;

import com.hmx.openplatform.commons.beans.ApiRoutingMQBean;
import com.hmx.openplatform.gateway.feign.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Description:
 *
 * @author hmx
 * @date Created on 2021/5/27
 */
@Component
public class ApiRoutingChangeListener {

    @Autowired
    private CacheService cacheService;

    @StreamListener("apiroutingchange")
    public void onMessage(ApiRoutingMQBean bean){
        //收到消息后我们做什么
        //根据消息的类型来区分
        switch(bean.getType()){
            case ADD:
            case UPDATE:
                try{
                    Map<Object,Object> map = cacheService.hGetAll(bean.getKey());
                    InitApiRouting.API_ROUTING_MAP_CACHE.put(bean.getKey(),map);//保存起来
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
                case DELETE :
                    InitApiRouting.API_ROUTING_MAP_CACHE.remove(bean.getKey());//从本地删除数据
                    break;
        }
    }
}
